package com.example.rentingapp.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.rentingapp.R;
import com.example.rentingapp.api.Api;
import com.example.rentingapp.api.ApiConfig;
import com.example.rentingapp.api.TtitCallback;
import com.example.rentingapp.enetity.InformationEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class NewInformationActivity extends BaseActivity implements View.OnClickListener {
    private Button btnCreate;
    private TextView tvQuit;
    private EditText etName, etSex, etAge, etPhone, etIdCard;
    private String informationUserId, name, sex, age, phone, idCard;
    private List<InformationEntity> datas = new ArrayList<>();

    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
            }
        }
    };

    @Override
    protected int initLayout() {
        return R.layout.activity_new_information;
    }

    @Override
    protected void initView() {
        btnCreate = findViewById(R.id.btn_create);
        tvQuit = findViewById(R.id.tv_quit);
        etName = findViewById(R.id.et_name);
        etSex = findViewById(R.id.et_sex);
        etAge = findViewById(R.id.et_age);
        etPhone = findViewById(R.id.et_phone);
        etIdCard = findViewById(R.id.et_id_card);
    }

    @Override
    protected void initData() {
        btnCreate.setOnClickListener(this);
        tvQuit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_create:
                newAData();//创建用户个人信息
                showToast("创建成功");
                navigateToWithFlag(LoginActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                break;
            case R.id.tv_quit:
                navigateToWithFlag(LoginActivity.class, Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                break;
        }
    }

    //创建用户个人信息
    private void newAData() {
        informationUserId = findByKey("user_id_register");
        name = etName.getText().toString().trim();
        sex = etSex.getText().toString().trim();
        age = etAge.getText().toString().trim();
        phone = etPhone.getText().toString().trim();
        idCard = etIdCard.getText().toString().trim();

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("information_user_id", informationUserId);
        params.put("name", name);
        params.put("sex", sex);
        params.put("age", age);
        params.put("phone", phone);
        params.put("id_card", idCard);
        Api.config(ApiConfig.NEW_INFORMATION, params).postRequest(mContext, new TtitCallback() {
            @Override
            public void onSuccess(final String res) {
                Log.e("onSuccess", res);
            }

            @Override
            public void onFailure(Exception e) {
                Log.e("onFailure", e.toString());
            }
        });
    }
}